package com.james.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.james.constant.CheckGroupDeleteFileByCheckitemException;
import com.james.constant.CheckGroupDeleteFileBySetmealException;
import com.james.dao.CheckGroupDao;
import com.james.entity.PageResult;
import com.james.entity.QueryPageBean;
import com.james.pojo.CheckGroup;
import com.james.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ACT
 */

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //新增检查组信息
        checkGroupDao.add(checkGroup);
        //获取自增id
        Integer id = checkGroup.getId();
        //设置检查组和检查项的关联关系
        setCheckGroupAndCheckItem(id, checkitemIds);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckGroup> page = checkGroupDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //根据检查组id删除中间表数据（清理原有关联关系）
        checkGroupDao.deleteAssociation(checkGroup.getId());
        //更新检查组基本信息
        checkGroupDao.edit(checkGroup);
        //向中间表插入数据（建立检查组和检查项关联关系）
        setCheckGroupAndCheckItem(checkGroup.getId(), checkitemIds);
    }

    @Override
    public void delete(Integer id) throws CheckGroupDeleteFileBySetmealException, CheckGroupDeleteFileByCheckitemException {
        Long setmealCount = checkGroupDao.findSetmealCountByCheckGroupId(id);
        if (setmealCount > 0) {
            throw new CheckGroupDeleteFileBySetmealException();
        }
        Long checkitemCount = checkGroupDao.findCheckitemCountByCheckGroupId(id);
        if (checkitemCount > 0) {
            throw new CheckGroupDeleteFileByCheckitemException();
        }
        checkGroupDao.deleteById(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    public void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkitemIds) {
        //设置检查组和检查项的关联关系
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id", checkGroupId);
                map.put("checkitem_id", checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }
}
